<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app';
import { ref } from 'vue';
import type { IConserveMember } from '@/simba/types/conserve.d';
import type { IUserInfo } from '@/simba/types/user.d';

	import { useConserve } from '@/simba/hooks/conserve';
	const { getConservePersonInfo, updateConserve, deleteConserve } = useConserve()
	const conservePersonInfo = ref<IConserveMember>({
		id: 0,
		userid: 0,
		conserveUserid: 0,
		nickname: '',
		mobile: '',
		avatar:'',
		remark: '',
		statusApprova: '0',
		refuseReason: ''
	})
	const userInfo = ref<IUserInfo>({
		id: 0,
		nickname: '',
		avatar: '',
		mobile: ''
	})
	const statusApprova = ref(0)
	onLoad(async (options?: AnyObject)=>{
		conservePersonInfo.value = await getConservePersonInfo(options.id);
		userInfo.value = conservePersonInfo.value
		conservePersonInfo.value.statusApprova = '1'
		// console.log('conservePersonInfo',  conservePersonInfo.value)
	})
	
	function onConserveApproveHandler(){
		if(conservePersonInfo.value.statusApprova == 2 && conservePersonInfo.value.refuseReason.length==0){
			uni.showToast({
				icon: 'none',
				title: '请输入拒绝理由'
			})
			return
		}
		updateConserve(conservePersonInfo.value)
		// console.log('onConserveApproveHandler',  conservePersonInfo.value)
	}
	// function onchangeHandler(val){
	// 	console.log('onConserveDeleteHandler', val, conservePersonInfo.value.statusApprova)
	// }
</script>

<template>
	<siScanQrCode :userInfo="userInfo">
		<template>
			<view class="">
				<view class="remark" v-if="statusApprova==0">
					<span style="">备注</span>
					<up-textarea v-model="conservePersonInfo.remark" disabled></up-textarea>
					
					<view style="margin-top: 60rpx;">
						<up-radio-group v-model="conservePersonInfo.statusApprova as string" placement="row">
							<up-radio activeColor="#00BB99" key="1" label="同意" name="1"></up-radio>
							<up-radio activeColor="#00BB99" key="2" label="拒绝" name="2"></up-radio>
						</up-radio-group>
						<up-textarea 
							v-if="conservePersonInfo.statusApprova==2" 
							v-model="conservePersonInfo.refuseReason"
							placeholder="请输入拒绝理由"
							count maxlength="20"></up-textarea>
					</view>
				<!-- </view>
				<view v-if="conservePersonInfo.statusApprova==0"> -->
					<up-button type="primary" text="确认审批" @tap="onConserveApproveHandler"></up-button>
					<!-- <up-button type="error" :plain="true" text="拒绝" @tap="onConserveApproveHandler(2)"></up-button> -->
				</view>
				<view v-else-if="statusApprova==1">
					<!-- <up-button type="primary" :plain="true" text="删除" @tap="onConserveDeleteHandler"></up-button> -->
					<up-button type="primary" :plain="true" text="删除" @tap="deleteConserve(conservePersonInfo.id)"></up-button>
					
				</view>
			</view>
		</template>
	</siScanQrCode>
</template>

<style lang="scss" scoped>
	.user{
		background-color: #fff;
		margin: 10rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		.info{
			width: 100%;
			display: flex;
			flex-direction: column;
			justify-content: start;
	
			border: red 1rpx solid;
		}
	}
</style>
